<template>
  <div>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="合同编号" prop="contractNum">
          <el-input
            v-model="queryParams.contractNum"
            placeholder="请输入合同编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="合同名称" prop="contractName">
          <el-input
            v-model="queryParams.contractName"
            placeholder="请输入合同名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="日期范围">
          <el-date-picker
            v-model="queryParams.dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:post:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:post:edit']"
          >修改
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </div>
    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="合同名称" align="center" prop="contractName"/>
      <el-table-column label="甲方名称" align="center" prop="firstName"/>
      <el-table-column label="乙方名称" align="center" prop="secondName"/>
      <el-table-column label="乙方负责人" align="center" prop="secondPerson"/>
      <el-table-column label="乙方电话" align="center" prop="secondPhone"/>
      <el-table-column label="合同金额" align="center" prop="contractPrice"/>
      <el-table-column label="签订日期" align="center" prop="signTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="contractStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_status" :value="scope.row.contractStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="selectDaily(scope.row)"
            v-hasPermi="['system:post:edit']"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:remove']"
          >修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!--    添加框-->
    <el-dialog :title="title" align="left" :visible.sync="open" width="12 00px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="合同编号" prop="contractCode">
              <el-input v-model="form.contractNum" placeholder="请输入合同编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="项目名称" prop="contractName">
              <el-input v-model="form.contractName" placeholder="请输入项目名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="乙方名称" prop="secondName">
              <el-input v-model="form.secondName" placeholder="请输入乙方名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人" prop="secondPerson">
              <el-input v-model="form.secondPerson" placeholder="请输入乙方负责人"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="电话" prop="secondPhone">
              <el-input v-model="form.secondPhone" placeholder="请输入乙方电话"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="甲方名称" prop="firstName">
              <el-input v-model="form.firstName" placeholder="请输入甲方名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人" prop="firstPerson">
              <el-input v-model="form.firstPerson" placeholder="请输入甲方负责人"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="电话" prop="firstPhone">
              <el-input v-model="form.firstPhone" placeholder="请输入甲方电话"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="项目金额" prop="contractPrice">
              <el-input v-model="form.contractPrice" placeholder="请输入项目金额"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <!--        签订日期-->
            <el-form-item label="签订日期" prop="signTime">
              <el-date-picker
                v-model="form.signTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择签订日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="质保期" prop="validity">
              <el-input v-model="form.validity" placeholder="请输入质保期"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="状态" prop="contractStatus" v-if="form.contractId != undefined">
          <el-select v-model="form.contractStatus" placeholder="合同状态" clearable>
            <el-option
              v-for="dict in dict.type.project_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              :keyup.enter.native="handleQuery"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-upload
          class="upload-demo"
          action="https://jsonplaceholder.typicode.com/posts/"
          :on-change="handleChange"

          :file-list="fileList">
          附件
          <el-button size="small" type="primary">选取文件</el-button>
          <div slot="tip" class="el-upload__tip">请上传 大小不超过 100MB 格式为 doc/docx/xls/ppt/txt/pdf 的文件</div>
        </el-upload>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">添加</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="合同详情" :visible.sync="lookDaily" width="60%" custom-class="colored-dialog">
      <div class="colored-dialog">
      <el-descriptions :column="3"  border>
        <el-descriptions-item label="合同名称">{{ form.contractName }}</el-descriptions-item>
        <el-descriptions-item label="合同编号">{{ form.contractNum }}</el-descriptions-item>
        <el-descriptions-item label="甲方名称">{{ form.firstPartyName }}</el-descriptions-item>
        <el-descriptions-item label="甲方负责人">{{ form.firstName }}</el-descriptions-item>
        <el-descriptions-item label="甲方电话">{{ form.firstPhone }}</el-descriptions-item>
        <el-descriptions-item label="乙方名称">{{ form.secondPartyName }}</el-descriptions-item>
        <el-descriptions-item label="乙方负责人">{{ form.secondPerson }}</el-descriptions-item>
        <el-descriptions-item label="乙方电话">{{ form.secondPartyPhone }}</el-descriptions-item>
        <el-descriptions-item label="合同金额">{{ form.contractPrice }}</el-descriptions-item>
        <el-descriptions-item label="签订日期">{{ form.signTime }}</el-descriptions-item>
        <el-descriptions-item label="有效期">{{ form.validity }}</el-descriptions-item>
        <el-descriptions-item label="备注" :column-span="3">{{ form.remark }}</el-descriptions-item>
      </el-descriptions>
      </div>
      <span slot="footer" class="dialog-footer">

    </span>
    </el-dialog>
  </div>
</template>

<script>
import dict from "@/utils/dict";
import {addDaily, getDaily, listDaily, selectDailyById,} from "@/api/kobe/daily/daily";
import {updateData} from "@/api/system/dict/data";

export default {
  name: "daily",
  dicts: ['project_status'],
  data() {
    return {
      //查看表格
      lookDaily: false,

      fileList: [],
      // 日期范围
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractNum: undefined,
        contractType: undefined,
        contractStatus: undefined,
        dateRange: [],
        contractName: undefined,
        firstPartyName: undefined,
        secondPartyName: undefined,
        secondPartyManager: undefined,
        secondPartyPhone: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        contractName: [
          {required: true, message: "项目名称不能为空", trigger: "blur"}
        ],
        contractNum: [
          {required: true, message: "合同编号不能为空", trigger: "blur"}
        ],
        secondName: [
          {required: true, message: "乙方名称不能为空", trigger: "blur"}
        ],
        secondPerson: [
          {required: true, message: "乙方负责人不能为空", trigger: "blur"}
        ],
        secondPhone: [
          {required: true, message: "乙方电话不能为空", trigger: "blur"}
        ],
        firstName: [
          {required: true, message: "甲方名称不能为空", trigger: "blur"}
        ],
        firstPerson: [
          {required: true, message: "甲方负责人不能为空", trigger: "blur"}
        ],
        firstPhone: [
          {required: true, message: "甲方电话不能为空", trigger: "blur"}
        ],
        validity: [
          {required: true, message: "质保期不能为空", trigger: "blur"}
        ],
        signTime: [
          {required: true, message: "签订日期不能为空", trigger: "blur"}
        ],
        contractPrice: [
          {required: true, message: "项目金额不能为空", trigger: "blur"}
        ],
        // 移除contractType的验证规则，因为表单中没有这个字段
        remark: [
          {required: true, message: "备注不能为空", trigger: "blur"}
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleChange(file, fileList) {
      this.fileList = fileList.slice(-3);
    },
    dict,
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      listDaily(this.queryParams).then(response => {
        this.postList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        contractNum: undefined,
        contractId: undefined,
        contractType: undefined,
        dateRange: [],
        contractStatus: undefined,
        contractName: undefined,
        secondName: undefined,
        secondPerson: undefined,
        secondPhone: undefined,
        firstName: undefined,
        firstPerson: undefined,
        firstPhone: undefined,
        contractPrice: undefined,
        signTime: undefined,
        validity: undefined,
        remark: undefined
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()

    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.contractId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "合同项目"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      // this.form = row
      // this.open = true
      // this.title = "修改合同"

      const postId = row.contractId || (this.ids.length > 0 ? this.ids[0] : null)
      if (postId) {
        console.log(postId)
        selectDailyById({contractId: postId}).then(response => {
          this.form = response.data
          this.open = true
          this.title = "修改合同"
        })
      }
    },
    /**查看按钮操作*/
    selectDaily(row) {
      this.form = row
      this.lookDaily = true
      this.title = "查看合同"
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.contractId != undefined) {
            getDaily(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addDaily(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style scoped>
.colored-dialog {
  --label-bg: #f0f9eb; /* 淡绿色标签背景 */
  --label-color: #606266; /* 标签文字颜色 */
}

.custom-descriptions {
  width: 100%;
}

/* 标签列样式（淡绿色背景、居中对齐、固定宽度） */
::v-deep .el-descriptions-item__label {
  background-color: var(--label-bg);
  color: var(--label-color);
  text-align: center;
  width: 120px;
}

/* 内容列样式（左侧内边距） */
::v-deep .el-descriptions-item__content {
  padding-left: 10px;
}

/* 图标与文字间距 */
.el-icon {
  margin-right: 5px;
}

/* 底部按钮右对齐 */
.dialog-footer {
  text-align: right;
}
</style>
